using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace jivemessenger
{
    [Service]
    public class JiveGCMIntentService : IntentService
    {
        static PowerManager.WakeLock sWakeLock;
        static object LockObject = new object();

       public static void RunIntentInService(Context context, Intent intent)
        {
            lock (LockObject)
            {
                if (sWakeLock == null)
                {
                    // This is called from BroadcastReceiver, there is no init.
                    var pm = PowerManager.FromContext(context);
                    sWakeLock = pm.NewWakeLock(
                    WakeLockFlags.Partial, "My WakeLock Tag");
                }
            }

            sWakeLock.Acquire();
            intent.SetClass(context, typeof(JiveGCMIntentService));
            context.StartService(intent);
        }

        protected override void OnHandleIntent(Intent intent)
        {
            try
            {
                Context context = this.ApplicationContext;
                string action = intent.Action;

                if (action.Equals("com.google.android.c2dm.intent.REGISTRATION"))
                {
                    HandleRegistration(context, intent);
                }
                else if (action.Equals("com.google.android.c2dm.intent.RECEIVE"))
                {
                    HandleMessage(context, intent);
                }
            }
            finally
            {
                lock (LockObject)
                {
                    //Sanity check for null as this is a public method
                    if (sWakeLock != null)
                        sWakeLock.Release();
                }
            }
        }

        private void HandleRegistration(Context context, Intent intent)
        {
            string registrationId = intent.GetStringExtra("registration_id");
            string error = intent.GetStringExtra("error");
            string unregistration = intent.GetStringExtra("unregistered");

            Bundle bundle = intent.Extras;
            if (bundle != null)
            {
                Messenger messenger = (Messenger)bundle.Get("messenger");
                // do something with the score

                Message message = Message.Obtain();
                Bundle b = new Bundle();
                b.PutString("registrationId", registrationId);
                b.PutString("error", error);
                b.PutString("unregistration", unregistration);

                message.Data = b;
                messenger.Send(message);
            }
        }

        private void HandleMessage(Context context,Intent intent)
        {
            string msg = intent.GetStringExtra("msg");
               Bundle bundle = intent.Extras;
            if (bundle != null)
            {
                Messenger messenger = (Messenger) bundle.Get("messenger");
                // do something with the score

                Message message = Message.Obtain();
                Bundle b = new Bundle();
                b.PutString("InputText", msg);

                message.Data = b;
                messenger.Send(message);
            }

           
        }
    }
}